package com.example.controller;

import com.example.common.Result;
import com.example.entity.MissBook;
import com.example.service.MissBookService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 帖子前端接口
 */
@RestController
@RequestMapping("/missbook")
public class MissBookController {

    @Resource
    private MissBookService missBookService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody MissBook missBook) {
        List<MissBook> list = missBookService.selectAll(missBook);

        if (list != null && !list.isEmpty()) {
            // 如果List有结果，则退出
            return Result.success("List already contains data. Exiting without adding.");
        } else {
            // 否则执行添加操作
            missBookService.add(missBook);
            return Result.success("Data added successfully.");
        }

    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        missBookService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        missBookService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody MissBook missBook) {
        missBookService.updateById(missBook);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        MissBook missBook = missBookService.selectById(id);
        return Result.success(missBook);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll( MissBook missBook) {
        List<MissBook> missBookList = missBookService.selectAll(missBook);
        return Result.success(missBookList);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(MissBook missBook,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<MissBook> pageInfo = missBookService.selectPage(missBook, pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
